package com.springtiles.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;

import com.springtiles.entity.Userinfo;

/**
 * 
 * ErrorController只处理一个URL
 * http://www.ibm.com/developerworks/cn/java/j-lo-spring25-mvc/
 * 
 * @author Jimmy.Qiu
 * 
 */
@Controller
@RequestMapping("/error.do")
// ①将ModelMap中属性名为currUser的属性
@SessionAttributes("currUser")
// @SessionAttributes(types = {User.class})
// @SessionAttributes(types = {User.class,Dept.class},value={“attr1”,”attr2”})。
public class ErrorController {
	Logger log = LoggerFactory.getLogger(ErrorController.class);

	// http://xxxx/error.do?%$^&*()
	// 默认的处理方法
	@RequestMapping
	public String showhello() {
		log.info("showhello");
		return "error";
	}

	// 如果URL请求中包括"method=listerrors"的参数，由本方法进行处理
	// http://xxxx/error.do?method=listerrors
	@RequestMapping(params = "method=listerrors")
	public String listerrors() {
		log.info("listerrors");
		return "error";
	}

	// 如果URL请求中包括"method=abcd"的参数，由本方法进行处理
	// http://xxxx/error.do?method=abcd
	@RequestMapping(params = "method=abcd")
	public String xxxxxxxxxxxx() {
		log.info("xxxxxxxxxxxx");
		return "error";
	}

	// 如果URL请求中包括"method=posttest"的参数，且mehto == "post" 由本方法进行处理
	// http://xxxx/error.do?method=posttest
	@RequestMapping(params = "method=posttest", method = RequestMethod.POST)
	public String post() {
		log.info("xxxxxxxxxxxx");
		return "error";
	}

	// 参数的自动匹配
	// http://xxxx/error.do?method=public&id=1111
	@RequestMapping(params = "method=public", method = RequestMethod.GET)
	public String arg(int id) {
		log.info("id : {}", id);
		return "error";
	}

	// 参数的自动匹配
	// http://xxxx/error.do?method=public&id=1111&name=jimmy
	@RequestMapping(params = "method=publics", method = RequestMethod.GET)
	public String args(int id, String name) {
		log.info("id : {} name : {}", id, name);
		return "error";
	}

	// 参数的自动匹配
	// JavaBean 自动匹配
	// http://127.0.0.1:8080/springtiles/error.do?method=fufu&userid=1111&username=jimmy&userId=323&userName=43434
	@RequestMapping(params = "method=fufu", method = RequestMethod.GET)
	public String fufu(int userid, String username, Userinfo user) {
		log.info("id & name: {}  : user : {}", userid + username,
				user.toString());
		return "error";
	}

	// 参数的自动匹配
	// JavaBean 自动匹配
	// @RequestParam 注解指定
	// http://127.0.0.1:8080/springtiles/error.do?method=fufu2&id=1111&name=jimmy&userId=323&userName=43434
	@RequestMapping(params = "method=fufu2", method = RequestMethod.GET)
	public String fufu2(@RequestParam("id") int userid,
			@RequestParam("name") String username, Userinfo user) {
		log.info("id & name: {}  : user : {}", userid + username,
				user.toString());
		return "error";
	}

	// http://127.0.0.1:8080/springtiles/error.do?method=showuser&id=1111&name=jimmy&userId=323&userName=43434
	@RequestMapping(params = "method=showuser", method = RequestMethod.GET)
	public String showuser(@RequestParam("id") int userid,
			@RequestParam("name") String username, Userinfo user, ModelMap model) {
		log.info("id & name: {}  : user : {}", userid + username,
				user.toString());
		// 将user对象以currUser为键放入到model中
		model.addAttribute("currUser", user);
		return "user";
	}

	// @ModelAttribute session中值给user赋值
	// http://127.0.0.1:8080/springtiles/error.do?method=showuser2
	@RequestMapping(params = "method=showuser2", method = RequestMethod.GET)
	public String showuser2(@ModelAttribute("currUser") Userinfo user,
			ModelMap model) {
		log.info("showuser2 user : {}", user);
		// model.addAttribute("currUser", user);
		return "user";
	}

	// 其它用法
	@RequestMapping(params = "method=request", method = RequestMethod.GET)
	public String request(SessionStatus status) {
		log.info("request" + status.isComplete());
		// 通过调用 status.setComplete() 方法，该 Controller 所有放在 session 级别的模型属性数据将从
		// session 中清空。
		status.setComplete();
		// return "user";
		return "redirect:error.do";
	}
}
